package picobot.gui;

import java.io.File;
import java.util.Arrays;
import java.util.Set;

/**
 * This is a class for utilities (like file paths), especially a class for
 * storing and managing the image paths used in PicoPanel
 */
public class PicoUtility {

	/**
	 * default constructor
	 */
	public PicoUtility() {
		super();
	}

	/**
	 * a constructor that sets the skin to the default skin
	 * 
	 * @param mode
	 */
	public PicoUtility(String mode) {
		super();
		if (mode == "setDefault") {
			setDefaultSkin();
		}
	}

	private String iconVisited = null;
	private String iconBot = null;
	private String iconFree = null;
	private String iconWall = null;
	private String defaultRule = "rules/default-rule.rule";

	public final static String map = "map";
	public final static String rule = "rule";

	// default location path for our default skin
	private final static String defaultLocation = "skins/defaultSkin/";

	/**
	 * gets the icon path for the wall
	 * 
	 * @return the iconVisited
	 */
	public String getIconVisited() {
		return iconVisited;
	}

	/**
	 * sets the icon path for the visited cell
	 * 
	 * @param iconVisited
	 *            the iconVisited to set
	 */
	public void setIconVisited(String iconVisited) {
		this.iconVisited = iconVisited;
	}

	/**
	 * gets the icon for the bot
	 * 
	 * @return the iconBot
	 */
	public String getIconBot() {
		return iconBot;
	}

	/**
	 * sets the icon for the bot
	 * 
	 * @param iconBot
	 *            the iconBot to set
	 */
	public void setIconBot(String iconBot) {
		this.iconBot = iconBot;
	}

	/**
	 * gets the icon for the free cell
	 * 
	 * @return the iconFree
	 */
	public String getIconFree() {
		return iconFree;
	}

	/**
	 * sets the icon for the free cell
	 * 
	 * @param iconFree
	 *            the iconFree to set
	 */
	public void setIconFree(String iconFree) {
		this.iconFree = iconFree;
	}

	/**
	 * gets the icon for the wall
	 * 
	 * @return the iconWall
	 */
	public String getIconWall() {
		return iconWall;
	}

	/**
	 * sets the icon for the wall
	 * 
	 * @param iconWall
	 *            the iconWall to set
	 */
	public void setIconWall(String iconWall) {
		this.iconWall = iconWall;
	}

	/**
	 * gets the default Rule
	 * 
	 * @return the defaultRule
	 */
	public String getDefaultRule() {
		return defaultRule;
	}

	/**
	 * sets the default rule
	 * 
	 * @param defaultRule
	 *            the defaultRule to set
	 */
	public void setDefaultRule(String defaultRule) {
		this.defaultRule = defaultRule;
	}

	/**
	 * This method simply sets a default skin to our environment.
	 */
	public void setDefaultSkin() {
		setNewSkin(defaultLocation);
	}

	/**
	 * This method sets a new skin and is also used for setting up the default
	 * skin. It throws a NullPointerException if anything goes wrong.
	 * 
	 * @param path
	 *            is the URI path of the skin
	 */
	public void setNewSkin(String path) {
		try {
			this.setIconVisited(path + "traversed.png");
			this.setIconBot(path + "bot.png");
			this.setIconFree(path + "free.png");
			this.setIconWall(path + "wall.png");
		} catch (NullPointerException e) {
			e.getStackTrace();
		}
	}

	/**
	 * returns the extension of a file
	 * 
	 * @param f
	 *            is the file the extension wants to be known
	 * @return the extension name as a string
	 */
	public static String getExtension(File f) {
		String extension = null;
		String s = f.getName();
		int i = s.lastIndexOf('.');

		if (i > 0 && i < s.length() - 1) {
			extension = s.substring(i + 1).toLowerCase();
		}
		return extension;
	}

	/**
	 * checks if an integer array is contained in a set of integer arrays
	 * (integer arrays are in this case pairs of coordinates)
	 * 
	 * @param set
	 *            is the set that should contain the given set
	 * @param array
	 *            is the array that should be contained in the set
	 * @return true if the array is contained in the set, false otherwise
	 */
	public static boolean isArrayInSet(Set<int[]> set, int[] array) {
		boolean check = false;

		for (int[] element : set) {
			if (Arrays.equals(array, element)) {
				check = true;
			}
		}

		return check;
	}
}
